{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# State of Mind\n", "\n", "The idea of \"state\" is a bit like \"state of mind\"... there is a variable that keeps track of what you are currently doing.\n", "\n", "## Old Joke\n", "\n", "This reminds me of an old joke that perhaps hits a bit close to home:\n", "\n", "A professor is walking down the hallway and is stopped by a student.\n", "\n", "She is interested in discussing a project with the professor over lunch. \"Have you had lunch yet?\" she asks the professor.\n", "\n", "\"I'm not sure... which way was I walking when you stopped me?\" the professor responds.\n", "\n", "# State\n", "\n", "The basic idea (and need) for state is driven because you are updating an object at many frames per second. That is, you are constantly calling methods like `object.draw()` and `object.move()` and you need to be able to move each object a bit in the manner that it currently desires.\n", "\n", "## Bouncing Balls\n", "\n", "We kept track of state with the bouncing ball examples by keeping track of their x, y positions and their velocities. But you can also keep track by using a name.\n", "\n", "## Moving People" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "format": "row" }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_4\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_4\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_4\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_4\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #4:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #4 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "class Person {\n", " int x;\n", " int y;\n", " String state;\n", " int speed;\n", " color mycolor;\n", " \n", " Person(int x, int y, String state) {\n", " this.x = x;\n", " this.y = y;\n", " this.state = state;\n", " this.speed = 1 + int(random(2));\n", " this.mycolor = color(random(255), random(255), random(255));\n", " }\n", " \n", " void draw() {\n", " fill(this.mycolor);\n", " if (state.equals(\"to right\")) {\n", " ellipse(this.x + 5, this.y - 5, 5, 5);\n", " } else { // \"to left\"\n", " ellipse(this.x, this.y - 5, 5, 5);\n", " }\n", " rect(this.x, this.y, 5, 10);\n", " }\n", " \n", " void move() {\n", " // Change state if necessary:\n", " if (this.x > width) {\n", " state = \"to left\";\n", " } else if (this.x < 0) {\n", " state = \"to right\"; \n", " }\n", " \n", " // Update position based on state:\n", " if (state.equals(\"to right\")) {\n", " this.x += this.speed;\n", " } else if (state.equals(\"to left\")) {\n", " this.x -= this.speed;\n", " }\n", " }\n", "}\n", "\n", "Person[] people = new Person[10];\n", "\n", "void setup() {\n", " size(500, 100);\n", " for (int i=0; i width) {\n", " state = \"to left\";\n", " } else if (this.x < 0) {\n", " state = \"to right\"; \n", " }\n", " \n", " // Update position based on state:\n", " if (state.equals(\"to right\")) {\n", " this.x += 1;\n", " } else if (state.equals(\"to left\")) {\n", " this.x -= 1;\n", " }\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can then use that file by %including it in a Processing sketch:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_5\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_5\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_5\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_5\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #5:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #5 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%include Person.java\n", "\n", "Person[] people = new Person[10];\n", "\n", "void setup() {\n", " for (int i=0; i